package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.CategoryVO;
import article.entities.IngredientVO;
import article.exceptions.BadCategoryException;
import article.exceptions.IncompleteArticleException;
import article.exceptions.MissingArticleException;
import article.exceptions.MissingCategoryException;

public class AddProductos extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text nombre;
	private Text precio;
	private Text descripcion;
	public IngredientVO[] ings;
	ArticleVO a;
	Label error;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AddProductos(Shell parent, int style, IngredientVO[] ing, ArticleVO art) {
		super(parent, style);
		ings=ing;
		setText("Agregar Producto");
		a=art;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(598, 324);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Label lblNombre = new Label(shell, SWT.NONE);
		FormData fd_lblNombre = new FormData();
		fd_lblNombre.top = new FormAttachment(0, 54);
		fd_lblNombre.left = new FormAttachment(0, 25);
		lblNombre.setLayoutData(fd_lblNombre);
		lblNombre.setText("Nombre");
		
		Label lblCategoria = new Label(shell, SWT.NONE);
		FormData fd_lblCategoria = new FormData();
		fd_lblCategoria.top = new FormAttachment(lblNombre, 27);
		fd_lblCategoria.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblCategoria.setLayoutData(fd_lblCategoria);
		lblCategoria.setText("Categoria");
		
		Label lblIngredientes = new Label(shell, SWT.NONE);
		FormData fd_lblIngredientes = new FormData();
		fd_lblIngredientes.top = new FormAttachment(lblCategoria, 19);
		fd_lblIngredientes.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblIngredientes.setLayoutData(fd_lblIngredientes);
		lblIngredientes.setText("Ingredientes");
		
		final Combo combo = new Combo(shell, SWT.NONE);
		FormData fd_combo = new FormData();
		fd_combo.top = new FormAttachment(lblCategoria, -4, SWT.TOP);
		fd_combo.left = new FormAttachment(lblCategoria, 21);
		combo.setLayoutData(fd_combo);
		Button btnNewButton = new Button(shell, SWT.NONE);
		combo.setVisible(true);

		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		CategoryVO [] cats =uso.getCategories();
		//System.out.println(cats.length);
		for (int i= 0;i<cats.length;i++){
		//	System.out.println(cats[i].getCat());
			combo.add(cats[i].getCat());
		}
		
		//BOTON INGREDIENTES
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String n = nombre.getText();
				String cat = combo.getText();
				//System.out.println(cat);
				String desc = descripcion.getText();
				double p;
				
				
				
				
				if(precio.getText().equalsIgnoreCase("")){
					p=-1;
				}
				else{
				p = Double.parseDouble(precio.getText());
				}
				ArticleProxyMgt art = ServiceFacade.getInstance().getArticleProxyMgt();
				CategoryVO[] cats = null;
				try {
					//System.out.println("tengo cats");
					cats = art.getCategories();
					//System.out.println(cats.length);
				} catch (RemoteException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (ProxyException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				CategoryVO c=null;
				for (int i = 0; i<cats.length;i++){
					if (cat.equalsIgnoreCase(cats[i].getCat())){
						c=cats[i];
					}
				}
				//System.out.println(c.getCat());
				//String name, String desc, double price, IngredientVO[] ingr, CategoryVO cat
				
				ArticleVO a = new ArticleVO(n,desc,p,ings,c);
				
				SelectIngredients s = new SelectIngredients (shell, shell.getStyle(), ings, a);
				shell.setVisible(false);
				try {
					s.open();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ProxyException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(lblIngredientes, -7, SWT.TOP);
		fd_btnNewButton.right = new FormAttachment(100, -43);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("Agregar Ingredientes");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.top = new FormAttachment(lblIngredientes, 0, SWT.TOP);
		fd_lblNewLabel.left = new FormAttachment(lblIngredientes, 6);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		
		if(ings==null){
		lblNewLabel.setText("Seleccione los ingredientes del nuevo producto");
		}
		else{
			String m = "";
			for (int i = 0; i<a.getIngr().length;i++){
			//	System.out.println(a.getIngr()[i].getIngr());
				//System.out.println("length" + ings.length);
				m= m + " " + ings[i].getIngr();
			}
			lblNewLabel.setText(m);
		}
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(lblIngredientes, 27);
		fd_lblNewLabel_1.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("Precio");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_2 = new FormData();
		fd_lblNewLabel_2.top = new FormAttachment(lblNewLabel_1, 27);
		fd_lblNewLabel_2.left = new FormAttachment(lblNombre, 0, SWT.LEFT);
		lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
		lblNewLabel_2.setText("Descripcion");
		
		nombre = new Text(shell, SWT.BORDER);
		fd_combo.right = new FormAttachment(nombre, 0, SWT.RIGHT);
		FormData fd_nombre = new FormData();
		fd_nombre.right = new FormAttachment(lblNombre, 152, SWT.RIGHT);
		fd_nombre.top = new FormAttachment(0, 49);
		fd_nombre.left = new FormAttachment(lblNombre, 30);
		nombre.setLayoutData(fd_nombre);
		
	
		
		precio = new Text(shell, SWT.BORDER);
		FormData fd_precio = new FormData();
		fd_precio.top = new FormAttachment(lblNewLabel_1, 0, SWT.TOP);
		fd_precio.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
		precio.setLayoutData(fd_precio);
		
		descripcion = new Text(shell, SWT.BORDER);
		FormData fd_descripcion = new FormData();
		fd_descripcion.right = new FormAttachment(lblNewLabel_2, 303, SWT.RIGHT);
		fd_descripcion.top = new FormAttachment(precio, 17);
		fd_descripcion.left = new FormAttachment(lblNewLabel_2, 10);
		descripcion.setLayoutData(fd_descripcion);
		

		
		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			//Boton Agregar
				public void widgetSelected(SelectionEvent e) {
				String n = nombre.getText();
				String cat = combo.getText();
			//	System.out.println("categoria"  + cat);
				String desc = descripcion.getText();
				double p = -10;
				try {
					 p = Double.parseDouble(precio.getText());
				//	 System.out.println(p);
			        } catch (NumberFormatException nfe) {
			        error.setText("ingrese un valor valido en el campo de precio");
			        }
				
				ArticleProxyMgt art = ServiceFacade.getInstance().getArticleProxyMgt();
				
				CategoryVO[] cats = null;
				try {
					//System.out.println("tengo cats");
					cats = art.getCategories();
					//System.out.println(cats.length);
				} catch (RemoteException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (ProxyException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				}
				CategoryVO c=null;
				ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				CategoryVO[] categ=null;
				try {
					categ = uso.getCategories();
				} catch (RemoteException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (ProxyException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				}
				for (int i = 0; i<categ.length;i++){
					if (cat.equalsIgnoreCase(categ[i].getCat())){
						c=categ[i];
					}
				}
				//System.out.println(c.getCat());
				ArticleVO artNew = new ArticleVO(n,desc,p,ings,c );
				try {
					art.addArticle(artNew);
					
					//CARTEL EXITO(!)
					shell.dispose();
					
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
				//	e1.printStackTrace();
				} catch (IncompleteArticleException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "No se Puede Agregar un Articulo con los datos Ingresados");
					ventana.open();
					shell.dispose();
					//e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					//e1.printStackTrace();
				} catch (MissingCategoryException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
				//	e1.printStackTrace();
				} catch (DataBaseErrorException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
				} catch (BadCategoryException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MissingArticleException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		FormData fd_btnNewButton_1 = new FormData();
		fd_btnNewButton_1.bottom = new FormAttachment(100, -10);
		fd_btnNewButton_1.left = new FormAttachment(0, 180);
		btnNewButton_1.setLayoutData(fd_btnNewButton_1);
		btnNewButton_1.setText("Agregar");
		
	 error = new Label(shell, SWT.NONE);
		FormData fd_error = new FormData();
		fd_error.right = new FormAttachment(0, 331);
		fd_error.bottom = new FormAttachment(btnNewButton_1);
		fd_error.left = new FormAttachment(0, 102);
		error.setLayoutData(fd_error);
		error.setText("");
		
		if(a!=null){
			nombre.setText(a.getName());
			if(a.getPrice()!=-1){
				precio.setText(String.valueOf(a.getPrice()));
			}
			descripcion.setText(a.getDesc());
		if(a.getCat()!=null){
			
			String cat = a.getCat().getCat();
			//System.out.println(cat);
			combo.setText(cat);
			if(cat.equalsIgnoreCase("")){
				
			}
			else{
				
			}
			}
		
		
		}
	}
}
